﻿using System;
using System.Configuration;

using LeanEngine.Extensions;
using LeanEngine;

namespace LeanEngine.Configuration
{
    /// <summary>
    /// Represents a proxy class for reading values from the configuration file.
    /// </summary>
    public struct AppSetting
    {
        /// <summary>
        /// Reads the value from the appSettings section.
        /// </summary>
        /// <exception cref="System.Configuration.ConfigurationErrorsException">Throwed when the key is not found on the configuration file.</exception>
        /// <param name="key">The key value of the parameter.</param>
        public static string GetValue(string key)
        {
            var value = ConfigurationManager.AppSettings[key];
            if (value.IsNullOrEmpty())
            {
                throw new ConfigurationErrorsException(string.Format(LeanLob_Core_Messages.AppSettingKeyNotFound, key));
            }

            return value;
        }

        /// <summary>
        /// Reads the value from the connectionString section.
        /// </summary>
        /// <exception cref="System.Configuration.ConfigurationErrorsException">Throwed when the key is not found on the configuration file.</exception>
        /// <param name="key">The key value of the parameter.</param>
        public static ConnectionStringSettings GetConnection(string key)
        {
            var value = ConfigurationManager.ConnectionStrings[key];
            if (value.IsNull() || value.ProviderName.IsNullOrEmpty() || value.ConnectionString.IsNullOrEmpty())
            {
                throw new ConfigurationErrorsException(string.Format(LeanLob_Core_Messages.AppSettingKeyNotFound, key));
            }

            return value;
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}
